package com.alex.j2se.stream;

import org.junit.Assert;
import org.junit.Test;

import java.util.Optional;

/**
 * Author  : gaojun            <br/>
 * Date    : 2020/7/22                          <br/>
 * Time    : 10:03 上午                         <br/>
 * ---------------------------------------    <br/>
 * Desc    :
 */
public class OptionalTest {

    @Test
    public void nullOrElseTest() {
        String targetStr = null;
        Assert.assertEquals("else", Optional.ofNullable(targetStr).orElse("else"));
    }

    @Test
    public void notNullOrElseTest() {
        String targetStr = "target";
        Assert.assertEquals("target", Optional.ofNullable(targetStr).orElse("else"));
    }

    @Test
    public void ifPresentPresentTest() {
        Person person = new Person(1, "alex");
        Person person2 = new Person(1, "alexjun");
        Optional.ofNullable(person).ifPresent(p -> person2.setName(p.getName()));
        Assert.assertEquals("alex", person2.getName());
    }

    @Test
    public void ifPresentNoTest() {
        Person person = null;
        Person person2 = new Person(1, "alexjun");
        Optional.ofNullable(person).ifPresent(p -> person2.setName(p.getName()));
        Assert.assertEquals("alexjun", person2.getName());
    }
}
